package com.hugesoft.jxgs.activity;

import java.util.List;

import org.apache.poi.hdf.extractor.Utils;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.hugesoft.jxgs.R;
import com.hugesoft.jxgs.activity.base.BaseActivity;
import com.hugesoft.jxgs.utils.CheckInputUtil;
import com.hugesoft.logic.managers.LogicManager;
import com.hugesoft.logic.managers.common.Constans;
import com.hugesoft.logic.module.Bus;
import com.hugesoft.logic.module.Traffic;
import com.hugesoft.utils.ExcelUtil;
import com.hugesoft.utils.Util;

public class SelectTypeActivity extends BaseActivity implements OnClickListener {
	private Button mSearchBusInfo, mBtnTraffic, mBtnBus, mBtnOutputExcel;
	private TextView mTextViewPoliceName, mTextViewLastLoginDate,
			mTextViewInfo;
	private EditText etPlateNumber;

	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activityselecttype);
		findViewById(R.id.backImg).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				finish();
			}
		});

		((TextView) findViewById(R.id.titleTextView)).setText("选择采集类别");
		mBtnBus = (Button) findViewById(R.id.btnbus);
		mBtnBus.setOnClickListener(this);
		// mBtnBus.setVisibility(View.GONE);
		mBtnTraffic = (Button) findViewById(R.id.btntraffic);
		mBtnTraffic.setOnClickListener(this);
		// mbtnTraffic.setVisibility(View.GONE);

		mSearchBusInfo = (Button) findViewById(R.id.btnSearchBusInfo);
		mSearchBusInfo.setOnClickListener(this);

		mTextViewPoliceName = (TextView) findViewById(R.id.textAccountName);
		mTextViewPoliceName.setText("欢迎您："
				+ LogicManager.policeManager().getcurrentpolice().getName()
				+ "同志");
		mTextViewLastLoginDate = (TextView) findViewById(R.id.textLastLoginDate);
		mTextViewLastLoginDate.setText("上次登陆时间"
				+ LogicManager.policeManager().getcurrentpolice()
						.getLastlogindate().toLocaleString());

		mBtnOutputExcel = (Button) findViewById(R.id.btnoutputexcel);
		mBtnOutputExcel.setOnClickListener(this);

		mTextViewInfo = (TextView) findViewById(R.id.txtInfo);

		etPlateNumber = (EditText) findViewById(R.id.etBusNo);
		etPlateNumber.addTextChangedListener(new TextWatcher() {
			@Override
			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
				searchhistory();
			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
			}

			@Override
			public void afterTextChanged(Editable s) {
			}
		});
	}

	@Override
	public void onClick(View v) {
		Intent intent = new Intent();
		switch (v.getId()) {
		case R.id.btnSearchBusInfo:
			searchhistory();
			break;
		case R.id.btnbus:
			if (checkplatenumber()) {
				intent.setClass(SelectTypeActivity.this, BusActivity.class);
				intent.putExtra(Constans.TAGPLATENUMBER, etPlateNumber
						.getText().toString());
				startActivityForResult(intent,Constans.REQUESTCODE_NEWBUS);
			}
			break;
		case R.id.btntraffic:
			if (checkplatenumber()) {
				intent.setClass(SelectTypeActivity.this, TrafficActivity.class);
				intent.putExtra(Constans.TAGPLATENUMBER, etPlateNumber
						.getText().toString());
				startActivityForResult(intent,Constans.REQUESTCODE_NEWTRAFFIC);

			}
			break;
		case R.id.btnoutputexcel:
			showWaitDialog("亲，稍等下");
			try {
				String path = ExcelUtil.createWorkBook();
				Util.showToast(SelectTypeActivity.this, "导出成功");
				mTextViewInfo.setText("文件路径为:" + path);
			} catch (Exception e) {
				Util.showToast(SelectTypeActivity.this, "导出失败");
				e.printStackTrace();
			}
			dismissDialog();
			break;
		default:
			break;
		}
	}

	private boolean checkplatenumber() {
		if (!CheckInputUtil.isbusnoverified(etPlateNumber.getText().toString())) {
			Util.showToast(SelectTypeActivity.this, "车牌号错了哦，亲");
			return false;
		}
		return true;
	}

	private void searchhistory() {
		String platenumber = etPlateNumber.getText().toString();
		if (CheckInputUtil.isbusnoverified(platenumber)) {
			List<Bus> buslist = LogicManager.busManager().loadbusinfo(
					platenumber);
			if (buslist != null && buslist.size() != 0) {
				if (buslist.size() == 1) {
					Intent intent = new Intent(SelectTypeActivity.this,
							BusActivity.class);
					intent.putExtra(Constans.TAGBUSID, buslist.get(0).getId());
					startActivity(intent);
				} else {
					Intent intent = new Intent(SelectTypeActivity.this,
							ListActivityBusInfo.class);
					intent.putExtra(Constans.TAGPLATENUMBER, platenumber);
					startActivity(intent);
				}
				mBtnBus.setEnabled(false);
				mBtnTraffic.setEnabled(false);
				return;
			} 
			List<Traffic> trafficlist = LogicManager.trafficManager().loadtrafficinfo(platenumber);

			if(trafficlist != null && trafficlist.size() != 0)
			{
				if (trafficlist.size() == 1) {
					Intent intent = new Intent(SelectTypeActivity.this,
							TrafficActivity.class);
					intent.putExtra(Constans.TAGTRAFFICID, trafficlist.get(0).getId());
					startActivity(intent);
				} else {
					Intent intent = new Intent(SelectTypeActivity.this,
							ListActivityTrafficInfo.class);
					intent.putExtra(Constans.TAGPLATENUMBER, platenumber);
					startActivity(intent);
				}
				mBtnBus.setEnabled(false);
				mBtnTraffic.setEnabled(false);
				return;
			}
			mBtnBus.setEnabled(true);
			mBtnTraffic.setEnabled(true);
			Util.showToast(SelectTypeActivity.this, "没有该车号的数据");
		} else {
			Util.showToast(SelectTypeActivity.this, "车号非法");
		}
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (data != null) {
			String platenumber = data.getStringExtra(Constans.TAGPLATENUMBER);
			if (platenumber.equalsIgnoreCase(etPlateNumber.getText().toString())) 
			{
				mBtnBus.setEnabled(false);
				mBtnTraffic.setEnabled(false);
			}
		}
		super.onActivityResult(requestCode, resultCode, data);
	
	}
}
